extends StaticBody2D
class_name BaseBridge

const BRIDGE_SIZE := Vector2(60,10)

const DissolveShader = preload("uid://chwuwneagkr2n")
@onready var sprite_2d: Sprite2D = $Sprite2D
@onready var collider_area: Area2D = $ColliderArea
@onready var collision_shape_2d: CollisionShape2D = $CollisionShape2D
@export var texture:Texture2D
@export var birdge_name:String


func _ready() -> void:
	GameEvents.stage_entered.connect(queue_free)
	var shape = RectangleShape2D.new()
	shape.size = BRIDGE_SIZE
	collision_shape_2d.shape = shape
	collider_area.body_entered.connect(on_body_entered)

func get_texture() -> Texture2D:
	return texture

func die() -> void:
	var tween = create_tween()
	tween.tween_property($Sprite2D.material,"shader_parameter/progress", 1.9,0.3).from(-1.3)\
	.set_ease(Tween.EASE_IN_OUT).set_trans(Tween.TRANS_CUBIC)
	tween.tween_callback(queue_free)

func on_body_entered(body: Player) -> void:
	if global_position.y - body.global_position.y < 60:
		body.global_position.y -= 20
	elif global_position.y - body.global_position.y < 120:
		body.global_position.y += 20
